import re
from plugins import CheckPlugin
from config import logger
from helper import CallProcess, raw2lines

formats = 'user,pid,%cpu,%mem,time,command'
cmd = '/bin/ps -axwwwo ' + formats

class Procstat(object):
    def _do_check(self, pat):
        proc = CallProcess(cmd)
        proc()
        lines = raw2lines(proc.stdout, 1)
        for line in lines:
            values = [l.strip() for l in line.split() if l.strip()]
            if not pat.match(values[-1]):
                continue

            self.results.append(dict(zip(formats.split(','), values)))

class CheckFSProcstat(CheckPlugin, Procstat):
    topic = 'fsprocstat'

    def check(self):
        logger.debug('%s check cmd: %s' % (self.topic, cmd))
        pat = re.compile(r'^_fs')
        self._do_check(pat)
